﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using QUANLYCHAMCONG.BL.BUS;
using QUANLYCHAMCONG.BL.Entities;
using Hopthoai;
namespace QUANLYCHAMCONG.PL.CMS.Nhanvien
{
    public partial class ImportNhanvienFromFile : UserControl
    {
        int flag = 0;
        int Num = 0;
        public delegate void _Close();
        public event _Close Close;
        frmHoidap f;
        public ImportNhanvienFromFile()
        {
            InitializeComponent();
        }

        private void ImportNhanvienFromFile_Load(object sender, EventArgs e)
        {
            this.toolbar1.SetToolBar(true, false, false, false, true, false, false, false);
            SetLink();
        }

        private void toolbar1_Close()
        {
            if (this.Close != null)
                this.Close();
        }

        private void toolbar1_Add()
        {
            this.flag = 1;
            this.toolbar1.SetToolBar(false, false, false, true, true, false, true, false);
            txtlink.Enabled = true;
            btnmo.Enabled = true;
            btndoc.Enabled = true;
            
        }
        private void SetLink()
        {
            this.links1.SetValues("Nhân viên", "Thêm nhân viên từ danh sách");
        }

        private void toolbar1_Refresh()
        {
            this.toolbar1.SetToolBar(true, false, false, false, true, false, false, false);
            txtlink.Enabled = false;
            btnmo.Enabled = false;
            btndoc.Enabled = false;
           
        }

        private void btnmo_Click(object sender, EventArgs e)
        {
            if (flag == 1)
            {
                try
                {
                    OpenFileDialog open = new OpenFileDialog();
                    open.Filter = "Excel File(*.XLS)|*.XLS|All files (*.*)|*.*";
                    open.Multiselect = false;
                    open.ShowDialog();
                    if (!String.IsNullOrEmpty(open.FileName))
                    {
                        txtlink.Text = open.FileName;
                    }
                }
                catch { return; }
            }
        }

        private void btndoc_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                if(!String.IsNullOrEmpty(txtlink.Text))
                {
                    dt = Common.GetExcelTable(txtlink.Text);
                    Num = dt.Rows.Count;
                    dgvdanhsachnv.DataSource = dt;
                }
            }
            catch (Exception ex)
            {
                f = new frmHoidap("Có lỗi khi mở file " + ex.Message, frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                f.ShowDialog();
            }
        }
        public Total UcLoad { get; set; }
        public void ReloadStattus()
        {
            try
            {
                Thongke tk = new Thongke();
                this.UcLoad.DtData = BAOCAOBUS.ThongkeTotal(tk);
                this.UcLoad.Settext();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        private void toolbar1_Save()
        {
            try
            {
                if (this.flag == 1)
                {
                    bool IsCommplete = true;
                    int count = 0;
                    // doc nv vao danh sach 
                    STAFFCollection ds = new STAFFCollection();
                    DataTable dt = new DataTable();
                    if (this.dgvdanhsachnv.Rows.Count != 0)
                    {
                        int i = 1;
                        foreach (DataGridViewRow dr in dgvdanhsachnv.Rows)
                        {
                            if (i <= Num)
                            {
                                STAFF nv = new STAFF();
                                //  nv.MANV = STAFFBUS.GetID();
                                nv.TENNV = dr.Cells["TENNV"].Value.ToString();
                                nv.MACCM = dr.Cells["MACC"].Value.ToString();
                                nv.TENCC = dr.Cells["TENCHAMCONG"].Value.ToString();
                                nv.UserEnabled = true;
                                if (dr.Cells["GIOITINH"].Value.ToString() == "Nam")
                                {
                                    nv.GIOITINH = true;
                                }
                                else
                                {
                                    nv.GIOITINH = false;
                                }
                                dt = STAFFBUS.CheckExitsStaff(nv);
                                if (dt.Rows.Count != 0)
                                {
                                    f = new frmHoidap("Nhân viên " + nv.TENNV.ToString() + "Đã tồn tại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                    f.ShowDialog();
                                    continue;
                                }
                                else
                                {
                                    ds.Add(nv);
                                }
                                i++;
                            }
                            else
                                return;
                        }
                        //thuc hien them nhan vien
                        //kiem tra van de nhan vien hoan toan da trong du lieu 
                        int numnv = ds.Count;
                        if (numnv > 0)
                        {
                            foreach (STAFF s in ds)
                            {
                                IsCommplete = STAFFBUS.Insert(null, s);
                                if (IsCommplete)
                                {
                                    count++;
                                }
                                else
                                    break;
                            }
                            if (IsCommplete)
                            {
                                f = new frmHoidap("Thêm danh sách nhân viên thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                                f.ShowDialog();
                                ReloadStattus();
                                this.toolbar1.SetToolBar(true, false, false, false, true, false, false, false);
                            }
                            else
                            {
                                int sonvxoadi = 0;
                                bool ketquaxoa = true;
                                foreach (STAFF xoa in ds)
                                {
                                    ketquaxoa = STAFFBUS.Delete(xoa);
                                    if (ketquaxoa)
                                        sonvxoadi++;
                                    if (sonvxoadi == count)
                                        break;

                                }
                                f = new frmHoidap("Thêm danh sách nhân viên Thất bại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                f.ShowDialog();
                                this.toolbar1.SetToolBar(true, false, false, false, true, false, false, false);
                            }
                        }
                        else
                        {
                            f = new frmHoidap("Tất cả các nhân viên đều đã tồn tại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                            f.ShowDialog();
                            this.toolbar1.SetToolBar(true, false, false, false, true, false, false, false);
                        }

                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
       
    }
}
